<template>
	<view class="content b-t" :class="'theme-'+theme">
	<view class="addTitle">
      <view class="titleDetail">
        <view class="titleLeft" > 
			<image src="../../static/newh5/fanhui.png" 
			class="yticon" 
			style="width: 13px;height: 20px;color:black" 
			@click="returnIndex()"
			></image>
        </view>
        <view class="titleCenter" >
          <b class="titleText">收货地址</b>
        </view>
        <view class="titleRight"  style="color: #269cd3;flex:1" > </view>
      </view>
    </view>
		<view class="list b-b" v-for="(item, index) in addrData.records" :key="index" @click="checkAddress(item)">
			<view class="wrapper">
				<view class="address-box">
					<text v-if="item.isDefault" class="tag themeTextColor themeBorderColor">默认</text>
					<text class="address" style="flex:1">{{item.areaName}} {{item.addr}}</text>
				</view>
				<view class="u-box">
					<text class="name">{{item.consigneeName}}</text>
					<text class="mobile">{{item.mobile}}</text>
				</view>
			</view>
			<text class="yticon icon-bianji" @click.stop="addAddress('edit', item)"></text>
			<image class="delete" mode="aspectFill" src="../../static/newh5/delete.png" @click.stop="delAddr(item)"></image>
		</view>
		<button class="add-btn themeBgColor" @click="addAddress('add')">新增地址</button>
	</view>
</template>

<script>
import {  goback } from "@/common/utils/navigateBack.js";
	import {
		getAddressList,
		deleteAddr
	} from '../../api/order/order.js'
	export default {
		data() {
			return {
				source: 0,
				addrPage: 1,
				addrData: {},
				addressList: [{
					name: '刘晓晓',
					mobile: '18666666666',
					addressName: '贵族皇仕牛排(东城店)',
					address: '北京市东城区',
					area: 'B区',
					default: true
				}, {
					name: '刘大大',
					mobile: '18667766666',
					addressName: '龙回1区12号楼',
					address: '山东省济南市历城区',
					area: '西单元302',
					default: false,
				}]
			}
		},
		onLoad(option) {
			this.source = option.source;
			this.loadAddrList()
		},
		onShow(){
			this.loadAddrList()
		},
		methods: {
			returnIndex() {
				goback()
			},
			//加载收货地址列表
			loadAddrList() {
				const params = {
					page: this.addrPage,
					limit: 100
				}
				getAddressList(params).then(res => {
					if (res.code === 0) {
						if (res.data !== null) {
							this.addrData = res.data;
						}
					} else {
						this.toast(res.message)
					}
				})
			},
			delAddr(item) {
				let params = {
					id: item.id
				}
				uni.showModal({
					title: '提示',
					content: '确定要删除吗？',
					confirmColor: '#000000',
					success: suc => {
						if (suc.confirm) {
							deleteAddr(params).then(res => {
								if (res.code === 0) {
									this.refreshList()
									this.$api.msg(`地址删除成功`);
								} else {
									this.toast(res.message)
								}
							})
						}
					}
				})
			},
			toast(msg) {
				uni.showToast({
					title: msg,
					duration: 1200,
					mask: false,
					icon: 'none'
				});
			},
			//选择地址
			checkAddress(item) {
				if (this.source === '1') {
					//下单时选择地址
					//this.$api.prePage()获取上一页实例，在App.vue定义
					this.$api.prePage().addressData = item;
					this.$api.prePage().calcFreight();
					// uni.navigateBack()
					this.backRouter()
				} else if (this.source === '2') {
					// 申请售后时选择地址
					this.$api.prePage().addressData = item;
					// uni.navigateBack()
					this.backRouter()
				}
			},
			backRouter() {   
                // #ifdef H5  
                let canBack = true  
                const pages = getCurrentPages()  
                // 有可返回的页面则直接返回，uni.navigateBack默认返回失败之后会自动刷新页面 ，无法继续返回  
                if (pages.length > 1) {  
                    uni.navigateBack(1)  
                    return;  
                }  
                // vue router 可以返回uni.navigateBack失败的页面 但是会重新加载  
                let a = this.$router.go(-1)  
                // router.go失败之后则重定向到首页  
                if (a == undefined) {  
                    uni.reLaunch({  
                        url: "/pages/index/index"  
                    })  
                }  
                return;  
                // #endif  
                uni.navigateBack(1)  
            },
			addAddress(type, item) {
				console.log('item',item);
				let data ={}
				data.item = item
				data.type = type
				uni.navigateTo({
					url: `/pages/address/addressManage?data=${JSON.stringify(data)}`
				})
			},
			//添加或修改成功之后回调
			refreshList() {
				this.loadAddrList()
			}
		}
	}
</script>

<style lang='scss'>
	page {
		padding-bottom: 120upx;
	}

	.content {
		position: relative;
	}

	.list {
		display: flex;
		align-items: center;
		padding: 20upx 30upx;
		;
		background: #fff;
		position: relative;
	}

	.wrapper {
		display: flex;
		flex-direction: column;
		flex: 1;
	}

	.delete {
		width: 40upx;
		height: 40upx;
		margin-left: 16upx;
	}

	.address-box {
		display: flex;
		align-items: center;
		  .tag {
			font-size: 24upx;
			width: fit-content;
			text-align: center;
			color: $my-newh5-color;
			margin-right: 10upx;
			background: #fffafb;
			border: 1px solid $my-newh5-color;
			border-radius: 4upx;
			padding: 4upx 10upx;
			line-height: 1;
		  }

		.address {
			font-size: 30upx;
			color: $font-color-dark;
		}
	}

	.u-box {
		font-size: 28upx;
		color: $font-color-light;
		margin-top: 16upx;

		.name {
			margin-right: 30upx;
		}
	}

	.icon-bianji {
		display: flex;
		align-items: center;
		height: 80upx;
		font-size: 40upx;
		color: $font-color-light;
		padding-left: 30upx;
	}

	.add-btn {
		position: fixed;
		left: 30upx;
		right: 30upx;
		bottom: 16upx;
		z-index: 95;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		font-size: 32upx;
		color: #fff;
		// background-color: $my-newh5-color;
		border-radius: 80rpx;
		/* box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4); */
	}
</style>
